package com.bdd.crowd.mvc.handler;

import com.bdd.crowd.entity.Role;
import com.bdd.crowd.service.api.RoleService;
import com.bdd.crowd.utils.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author banddong
 * @create 2020-09-12 22:30
 * Role(角色)Handler层
 */
@Controller
public class RoleHandler {

    /**
     * 装配service接口
      */
    @Autowired
    private RoleService roleService;


    /**
     * 页面一览
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword){

        // 调用service方法获取分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);;

        // 封装到ResultEntity对象中返回(如果上面的操作异常，交给异常映射机制处理CrowdExceptionResolver)
        return ResultEntity.successWithData(pageInfo);
        /*try {
            // 调用service方法获取分页数据
            pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
            return ResultEntity.successWithData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }*/
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/role/save.json")
    public ResultEntity<String> saveRole(Role role){

        roleService.saveRole(role);

        return ResultEntity.successWithoutData();
    }

    /**
     * 修改
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/role/update.json")
    public ResultEntity<String>updateRole(Role role){

        roleService.updateRole(role);

        return ResultEntity.successWithoutData();
    }

































}
